let activeEffect
const bucket = new WeakMap()
const data = {text:"hellow world"}

function effect(fn){
    // 注册副作用函数
    const effectFn = ()=>{
        cleanup(effectFn)
        activeEffect = fn
        fn()
    }
    effectFn.deps = []
    effectFn()
}

function cleanup(){

}

effect(
    ()=>{
        document.body.innerText = obj.text
    }
)


const obj = new Proxy(data,{
    get(target,key){
        track(target,key)
        return target[key]
    },
    set(target,newVal){
        target[key] == newVal
        trigger(target,key)
    }
})
// 追踪
function track(target,key){
    if(!activeEffect) return
    let depMap = bucket.get(target)
    if(!depMap){
        bucket.set(target,(depMap = new Map()))
    }
    let deps = depMap.get(key)
    if(!deps){
        deps.set(key,(dep = new Set()))
    }
    deps.add(activeEffect)
}
// 触发
function trigger(target,key){
    const depsMap = bucket.get(target)
    if(!depsMap)return
    const effects = depsMap.get(key)
    effects &&  effects.forEach(fn=> fn())
}